package cn.xo68.boot.auth.server.token.impl;

import cn.xo68.boot.auth.server.domain.OauthAuthorizationCode;
import cn.xo68.boot.auth.server.domain.OauthUser;
import cn.xo68.boot.auth.server.service.OauthAuthorizationCodeService;
import cn.xo68.boot.auth.server.token.AbstractTokenRequestCheck;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * AuthorizationCode模式的令牌申请审核
 * @author wuxie
 * @date 2018-8-10
 */
@Component
public class TokenRequestCheckOnAuthorizationCode extends AbstractTokenRequestCheck {

    private static final Logger logger= LoggerFactory.getLogger(TokenRequestCheckOnAuthorizationCode.class);

    @Autowired
    private OauthAuthorizationCodeService oauthAuthorizationCodeService;

    @Override
    public OauthUser checkCredentials() throws OAuthProblemException {
        OauthAuthorizationCode oauthAuthorizationCode=oauthAuthorizationCodeService.get(this.getOAuthTokenRequest().getCode());
        if(oauthAuthorizationCode ==null){
            throw OAuthProblemException.error("参数异常","参数不合法");
        }
        OauthUser oauth2User =getOauthUserService().getByUserId(oauthAuthorizationCode.getUserId());
        if(oauth2User ==null){
            throw OAuthProblemException.error("参数异常","参数不合法");
        }
        oauthAuthorizationCodeService.delete(this.getOAuthTokenRequest().getCode());
        return oauth2User;
    }
}
